package com.xbongbong.saas.enums;

import com.xbongbong.pro.enums.PackageTypeEnum;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author shen.yang
 * @version v1.0
 * @Description 关联表单枚举
 * @date 2022/2/22 14:21
 * @since v1.0
 */
@Getter
public enum FormTeamSetEnum {


    //客户
    CUSTOMER_MANAGEMENT(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), Arrays.asList(XbbRefTypeEnum.CONTACT.getCode(),XbbRefTypeEnum.CONTRACT.getCode(),XbbRefTypeEnum.SALES_OPPORTUNITY.getCode(),XbbRefTypeEnum.REFUND.getCode(),XbbRefTypeEnum.QUOTATION.getCode(),XbbRefTypeEnum.PAYMENT.getCode(),XbbRefTypeEnum.PAYMENT_SHEET.getCode()),
            Arrays.asList(XbbRefTypeEnum.CONTACT.getCode(),XbbRefTypeEnum.CONTRACT.getCode(),XbbRefTypeEnum.SALES_OPPORTUNITY.getCode(),XbbRefTypeEnum.REFUND.getCode(),XbbRefTypeEnum.QUOTATION.getCode(),XbbRefTypeEnum.PAYMENT.getCode())),
    //机会
    SALES_OPPORTUNITY(XbbRefTypeEnum.SALES_OPPORTUNITY.getCode(),Arrays.asList(XbbRefTypeEnum.CONTRACT.getCode(),XbbRefTypeEnum.QUOTATION.getCode()), null),
    //合同
    CONTRACT(XbbRefTypeEnum.CONTRACT.getCode(),Arrays.asList(XbbRefTypeEnum.REFUND.getCode(),XbbRefTypeEnum.PAYMENT.getCode(),XbbRefTypeEnum.PAYMENT_SHEET.getCode()),
            Arrays.asList(XbbRefTypeEnum.REFUND.getCode(),XbbRefTypeEnum.PAYMENT.getCode(),XbbRefTypeEnum.PAYMENT_SHEET.getCode())),
    //付款计划
    PAY_PLAN(XbbRefTypeEnum.PAY_PLAN.getCode(), Collections.singletonList(XbbRefTypeEnum.PAY_SHEET.getCode()), null),
    //供应商
    SUPPLIER(XbbRefTypeEnum.SUPPLIER.getCode(),Arrays.asList(XbbRefTypeEnum.SUPPLIER_CONTACT.getCode(),XbbRefTypeEnum.PURCHASE.getCode(),XbbRefTypeEnum.PAY_PLAN.getCode(),XbbRefTypeEnum.PAY_SHEET.getCode()), Arrays.asList(XbbRefTypeEnum.SUPPLIER_CONTACT.getCode())),
    //采购合同
    PURCHASE(XbbRefTypeEnum.PURCHASE.getCode(), Arrays.asList(XbbRefTypeEnum.PAY_PLAN.getCode(),XbbRefTypeEnum.PAY_SHEET.getCode()), Arrays.asList(XbbRefTypeEnum.PAY_PLAN.getCode(),XbbRefTypeEnum.PAY_SHEET.getCode())),
    //市场活动
    MARKET_ACTIVITY(XbbRefTypeEnum.MARKET_ACTIVITY.getCode(), Collections.singletonList(XbbRefTypeEnum.CLUE.getCode()), null),
    //线索
    //CLUE(XbbRefTypeEnum.CLUE.getCode(), Collections.singletonList(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode()), null),
    //经销商档案
    DISTRIBUTOR_MANAGEMENT(XbbRefTypeEnum.DISTRIBUTOR_MANAGEMENT.getCode(), null, Arrays.asList(XbbRefTypeEnum.CONTRACT.getCode(),XbbRefTypeEnum.CONTACT.getCode(),XbbRefTypeEnum.REFUND.getCode(),XbbRefTypeEnum.PAYMENT.getCode())),
    //订货单
    ORDER(XbbRefTypeEnum.ORDER.getCode(), null, Arrays.asList(XbbRefTypeEnum.REFUND.getCode(),XbbRefTypeEnum.PAYMENT.getCode(),XbbRefTypeEnum.PAYMENT_SHEET.getCode())),

    UNKNOWN(404,null, null);


    /**
     * 构造一个<code>XbbRefTypeEnum</code>枚举对象
     *
     * @param code  枚举Code
     * @param linkList  关联列表
     */
    FormTeamSetEnum(int code, List linkList, List noUltimateLinkList) {
        this.code = code;
        this.linkList = linkList;
        this.noUltimateLinkList = noUltimateLinkList;
    }

    /**
     * 枚举值 --
     */
    private final int code;


    /**
     *  关联列表
     */
    private final List<Integer> linkList;

    /**
     * 非旗舰版当前业务的关联业务
     */
    private final List<Integer> noUltimateLinkList;


    /**
     * 通过枚举 code 获得枚举
     *
     * @param code 枚举Code
     *
     * @return XbbRefTypeEnum
     */
    public static FormTeamSetEnum getByCode(Integer code) {
        for (FormTeamSetEnum cacheCode : values()) {
            if (Objects.equals(code, cacheCode.getCode())) {
                return cacheCode;
            }
        }
        return UNKNOWN;
    }

    /**
     * 根据业务类型获取当前业务的关联业务
     *
     * @param code 业务类型
     * @param ultimateTag 是不是旗舰版的标识
     * @param defaultRuleTag 是否用默认规则 true:默认 false:不默认
     * @return
     */
    public static List<Integer> getBusLinkList(Integer code, Boolean ultimateTag, boolean defaultRuleTag) {
        List<Integer> linkList = new ArrayList<>();
        for (FormTeamSetEnum formTeamSetEnum : FormTeamSetEnum.values()) {
           if (Objects.equals(code, formTeamSetEnum.getCode())) {
               if (defaultRuleTag) {
                   linkList = formTeamSetEnum.getNoUltimateLinkList();
               }else if (ultimateTag) {
                   linkList = formTeamSetEnum.getLinkList();
               }else {
                   linkList = formTeamSetEnum.getNoUltimateLinkList();
               }
           }
        }
        return linkList;
    }
    /**
     *
     * @Description: 判断当前表单是否支持继承列表，用于显隐
     * @param
     * @return: java.util.List<java.lang.Integer>
     * @Author: shen.yang
     * @Date: 2022/4/8  16:54
    */

    public static boolean checkLinkAble(Integer feeType, Integer businessType){
        if (Objects.equals(feeType, PackageTypeEnum.ULTIMATE.getType())) {
            //旗舰版，除开经销商的业务，其余业务都支持继承列表的编辑
            List<Integer> businessList = Stream.of(FormTeamSetEnum.values()).filter(v -> !Objects.equals(v, DISTRIBUTOR_MANAGEMENT) && !Objects.equals(v, ORDER)).map(FormTeamSetEnum::getCode).collect(Collectors.toList());
            if(businessList.contains(businessType)){
                return true;
            }
        }else if (Objects.equals(feeType, PackageTypeEnum.ADVANCED.getType())) {
            List<Integer> businessList = Arrays.asList(CUSTOMER_MANAGEMENT.getCode(), CONTRACT.getCode(), SUPPLIER.getCode(), PURCHASE.getCode());
            //高级版走老逻辑，只有客户、合同、供应商、采购合同支持继承列表编辑
            if(businessList.contains(businessType)){
                return true;
            }
        }
        return false;
    }

}
